import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/login'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'login',
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta:{
      roles: ['admin','user']
    },
  },
  {
    path: '/home',
    meta:{
      roles: ['admin','user']
    },
    component: () => import('../views/home/home'),
    children:[
      {
        path: '/',
        redirect: 'resourceManagement/list',
      },
      {
        path: "resourceManagement/list",
        name: "resource-management-list",
        meta:{
          roles: ['admin','user']
        },
        component: () => import('../views/resource/resource')
      },
      {
        path: "sceneManagement/list",
        name: "scene-management-list",
        meta:{
          roles: ['admin','user']
        },
        component: () => import('../views/subscribe/subscribe')
      },
      {
        path: 'subscribe',
        name: 'Subscribe',
        meta:{
          roles: ['admin']
        },
        component: () => import('../views/subscribe/subscribe')
      },
      {
        path: 'role',
        name: 'Role',
        meta:{
          roles: ['admin']
        },
        component: () => import('../views/role/role')
      }
    ]
  },
  {
    path:'/*',
    meta:{
      roles: ['admin','user']
    },
    component:() => import('../views/404')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const role = 'user'
router.beforeEach((to,from,next) => {
  // 1.页面权限
  // 1.角色权限不同的角色对应不同的页面权限有权限的可以直接进入，没权限的跳转到404
  if(to.meta.roles.includes(role)){
    next()
  }else{
    next('/404')
  }
  // 2.登录验证
  // 登录验证通过token,没有token跳转到登录页 有token直接跳转到原来的页面
  // if(sessionStorage.getItem('token')){
  //   // 通过登录页进来跳转到首页，有其他页面直接进入到其他页面
  //   if(to.path === '/login'){
  //     next('/')
  //   }else{
  //     next({path: to.path || '/'})
  //   }
  // }else{
  //   // 是登录页面跳转到登录页面 不是登录页面跳转到登录页面带上重定向的页面
  //   if(to.path === '/login'){
  //     next()
  //   }else{
  //     // next(`/login?redirect=${to.path}`)
  //     next()
  //   }
  // }
})

export default router
